import csv
import json
import time
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By

print("Begin testing...")
driver = webdriver.Chrome()
url = "https://www.jd.com"
driver.maximize_window()
driver.implicitly_wait(2)
# 访问网站，清空旧cookies信息
driver.get(url)
sleep(5)
driver.delete_all_cookies()

# 加载 cookies信息
with open("cookies.txt", "r") as f:
    cookies = json.load(f)
for cookie in cookies:
    driver.add_cookie(cookie)

# 验证是否登录成功
driver.get(url)
time.sleep(3)

# 从data_sku_list.csv读取商品ID列表
with open("../data_csv/product_id/data_sku_list.csv", "r") as f:
    reader = csv.reader(f)
    next(reader)  # 跳过表头
    sku_list = [row[0] for row in reader]

# 初始化CSV文件，写入表头
csv_file = "../data_csv/product_id/jd_product_details.csv"
with open(csv_file, mode='w', newline='', encoding='utf-8') as file:
    writer = csv.writer(file)
    # 写入表头
    writer.writerow(
        ["商品标题", "品牌", "商品名称", "CPU型号", "运行内存", "机身颜色", "三防标准", "充电功率", "支持IPv6",
         "屏幕材质", "后摄主像素", "机身内存", "屏幕分辨率", "泰尔电竞评级", "机身色系"])

# 爬取每个商品的信息
for sku in sku_list:
    product_url = f"https://item.jd.com/{sku}.html"
    print(product_url)
    driver.get(product_url)
    time.sleep(3)

    # 爬取商品名称
    sku_name = driver.find_element(By.CSS_SELECTOR, ".sku-name").text.strip()

    # 爬取商品品牌
    brand_element = driver.find_element(By.CSS_SELECTOR, "#parameter-brand li")
    brand = brand_element.get_attribute("title").strip()

    # 爬取商品详细参数
    parameters = driver.find_elements(By.CSS_SELECTOR, ".parameter2.p-parameter-list li")

    # 将参数存储到字典中以便写入CSV
    parameter_dict = {
        "商品标题": sku_name,
        "品牌": brand,
        "商品名称": "",
        "CPU型号": "",
        "运行内存": "",
        "机身颜色": "",
        "三防标准": "",
        "充电功率": "",
        "支持IPv6": "",
        "屏幕材质": "",
        "后摄主像素": "",
        "机身内存": "",
        "屏幕分辨率": "",
        "泰尔电竞评级": "",
        "机身色系": ""
    }

    for parameter in parameters:
        param_text = parameter.text.split("：")
        if len(param_text) == 2:
            key, value = param_text
            if key in parameter_dict:
                parameter_dict[key] = value.strip()

    # 将参数写入CSV
    with open(csv_file, mode='a', newline='', encoding='utf-8') as file:
        writer = csv.writer(file)
        writer.writerow([
            parameter_dict["商品标题"],
            parameter_dict["品牌"],
            parameter_dict["商品名称"],
            parameter_dict["CPU型号"],
            parameter_dict["运行内存"],
            parameter_dict["机身颜色"],
            parameter_dict["三防标准"],
            parameter_dict["充电功率"],
            parameter_dict["支持IPv6"],
            parameter_dict["屏幕材质"],
            parameter_dict["后摄主像素"],
            parameter_dict["机身内存"],
            parameter_dict["屏幕分辨率"],
            parameter_dict["泰尔电竞评级"],
            parameter_dict["机身色系"]
        ])

    print(f"Data for SKU {sku} saved to {csv_file}")

# 关闭浏览器
driver.quit()